package P02_sync;

public class SellTicket {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.sell();
                }
            }
        },"aa").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.sell();
                }
            }
        },"bb").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.sell();
                }
            }
        },"cc").start();
    }
}

class Ticket {
    private Integer number = 30;

    public synchronized void sell() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出：" + (number--) + " 剩下：" + number);
        }
    }
}
